package com.system.xingzhi.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.system.xingzhi.dao.mapper.ComponentMapper;
import com.system.xingzhi.dao.mapper.ComponentPermissionMapper;
import com.system.xingzhi.dao.mapper.PageMapper;
import com.system.xingzhi.dao.mapper.RolePermissionMapper;
import com.system.xingzhi.model.*;
import com.system.xingzhi.service.RolePermissionService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class RolePermissionServiceImpl implements RolePermissionService {

    @Autowired
    RolePermissionMapper rolePermissionMapper;

    @Autowired
    ComponentPermissionMapper componentPermissonMapper;

    @Autowired
    PageMapper pageMapper;

    @Autowired
    ComponentMapper componentMapper;

    @Override
    public ReturnData deleteByPrimaryKey(String rolePermissionId) {
        ReturnData returnData=new ReturnData();
        try {
            List<RolePermission> rolePermission=rolePermissionMapper.selectByParentId(rolePermissionId);
            if (rolePermission.size()!=0){
                throw new Exception("当前节点字节点数不为0，不能删除");
            }
            rolePermissionMapper.deleteByPrimaryKey(rolePermissionId);
            returnData.setCode(0);
            returnData.setDetail("删除节点成功");
        }catch (Exception e){
            e.printStackTrace();
            returnData.setCode(1);
            returnData.setDetail("删除节点失败");
            return returnData;
        }
        return returnData;
    }

    @Override
    public ReturnData insert(RolePermission record) {
        ReturnData returnData=new ReturnData();
        try {
            returnData.setData(rolePermissionMapper.insert(record));
            returnData.setCode(0);
            returnData.setDetail("添加角色成功");
        }catch (Exception e){
            returnData.setCode(1);
            returnData.setDetail("添加角色失败");
            return returnData;
        }
        return returnData;

    }

    @Override
    public int insertSelective(RolePermission record) {
        return rolePermissionMapper.insertSelective(record);
    }

    @Override
    public RolePermission selectByPrimaryKey(String rolePermissionId) {
        return rolePermissionMapper.selectByPrimaryKey(rolePermissionId);
    }

    @Override
    public int updateByPrimaryKeySelective(RolePermission record) {
        return rolePermissionMapper.updateByPrimaryKeySelective(record);
    }

    @Override
    public ReturnData updateByPrimaryKey(RolePermission record) {
        ReturnData returnData=new ReturnData();
        try {
            returnData.setData(rolePermissionMapper.updateByPrimaryKey(record));
            returnData.setCode(0);
            returnData.setDetail("更新角色角色成功");
        }catch (Exception e){
            e.printStackTrace();
            returnData.setCode(1);
            returnData.setDetail("更新角色失败");
            return returnData;
        }
        return returnData;
    }

    @Override
    public ReturnData selectByRoleId(String roleId) {
        ReturnData returnData = new ReturnData();
        try {
            returnData.setData(rolePermissionMapper.selectByRoleId(roleId));
            returnData.setCode(0);
            returnData.setDetail("更新角色角色成功");
        } catch (Exception e) {
            e.printStackTrace();
            returnData.setCode(1);
            returnData.setDetail("更新角色失败");
            return returnData;
        }
        return returnData;

    }

    @Override
    public ReturnData getOtherChildNode(String nodeId) {
        ReturnData returnData = new ReturnData();
        try{
            RolePermission rolePermission=rolePermissionMapper.selectByPrimaryKey(nodeId);
            List<RolePermission> childNode= rolePermissionMapper.selectByParentId(nodeId);
            if (rolePermission.getNodeType().equals("组件")){
                Map<String,String> map=new HashMap<>();
                map.put("componentId",rolePermission.getComponentId());
                List<ComponentPermission> list=componentPermissonMapper.selectByParameter(map);
                returnData.setData(getOtherComponentPermission(childNode,list));
            }else if (rolePermission.getNodeType().equals("目录")){
                List<Page> pages=pageMapper.selectByParentId(rolePermission.getPageId());
                returnData.setData(getOtherPage(childNode,pages));
            }else if(rolePermission.getNodeType().equals("根节点")){
                Map<String,String> map=new HashMap<>();
                map.put("typeName","根目录");
                List<Page> pages=pageMapper.selectByParameter(map);
                returnData.setData(getOtherPage(childNode,pages));
            }
            returnData.setCode(0);
            returnData.setDetail("获取子节点成功");
        }catch (Exception e){
            e.printStackTrace();
            returnData.setCode(1);
            returnData.setDetail("获取子节点失败");
        }
        return returnData;
    }

    @Override
    public ReturnData addChildNode(String nodeId, String id, String nodeType, String value) {
        ReturnData returnData = new ReturnData();
        try {
            RolePermission currentNode=rolePermissionMapper.selectByPrimaryKey(nodeId);
            if (nodeType.equals("根节点")||nodeType.equals("目录")){
                RolePermission childNode=new RolePermission();
                Page page=pageMapper.selectByPrimaryKey(id);
                childNode.setNodeName(page.getPageName());
                childNode.setNodeType("目录");
                childNode.setRoleName(currentNode.getRoleName());
                childNode.setRoleId(currentNode.getRoleId());
                childNode.setPageId(page.getPageId());
                childNode.setPageName(page.getPageName());
                childNode.setParentId(currentNode.getNodeId());
                rolePermissionMapper.insert(childNode);
                if (page.getComponentId()!=null){
                    RolePermission componentNode=new RolePermission();
                    Component component=page.getComponent();
                    componentNode.setNodeName(component.getComponentName());
                    componentNode.setNodeType("组件");
                    componentNode.setComponentId(component.getComponentId());
                    componentNode.setRoleId(currentNode.getRoleId());
                    componentNode.setRoleName(currentNode.getRoleName());
                    componentNode.setComponentName(componentNode.getComponentName());
                    componentNode.setParentId(childNode.getNodeId());
                    rolePermissionMapper.insert(componentNode);
                }
            }else if(nodeType.equals("组件")){
                RolePermission childNode=new RolePermission();
                ComponentPermission componentPermission=componentPermissonMapper.selectByPrimaryKey(id);
                childNode.setNodeName(componentPermission.getComponentPermissionName());
                childNode.setNodeType("权限属性节点");
                childNode.setRoleName(currentNode.getRoleName());
                childNode.setRoleId(currentNode.getRoleId());
                childNode.setComponentPermissionId(componentPermission.getComponentPermissionId());
                childNode.setComponentPermissionName(componentPermission.getComponentPermissionName());
                childNode.setParentId(currentNode.getNodeId());
                childNode.setNodeValue(value);
                rolePermissionMapper.insert(childNode);
            }
            returnData.setCode(0);
            returnData.setDetail("添加节点成功");
        }catch (Exception e){
            e.printStackTrace();
            returnData.setCode(1);
            returnData.setDetail("添加节点失败");
        }
        return returnData;
    }

    @Override
    public ReturnData getRouterTableByRole(String roleId) {
        ReturnData returnData = new ReturnData();
        try {
            List<RolePermission> rolePermissionsList = rolePermissionMapper.selectAllInfoByRoleId(roleId);
            returnData.setData(rolePermissionsList);
            returnData.setCode(0);
            returnData.setDetail("查询角色当前所有节点信息成功");
        } catch (Exception e) {
            e.printStackTrace();
            returnData.setCode(1);
            returnData.setDetail("查询角色当前所有节点信息成功");
            return returnData;
        }
        return returnData;
    }


    public   List<ComponentPermission> getOtherComponentPermission(List<RolePermission> childNode,List<ComponentPermission> list){
        List<ComponentPermission>  other= new ArrayList<>();
        for (int i=0;i<list.size();i++){
            boolean f=true;
            for (int j=0;j<childNode.size();j++){
                if(childNode.get(j).getNodeType().equals("权限属性节点")&&childNode.get(j).getComponentPermissionId().equals(list.get(i).getComponentPermissionId())){
                    f=false;
                }
            }
            if (f){
                other.add(list.get(i));
            }
        }
        return other;
    }
    public   List<Page> getOtherPage(List<RolePermission> childNode,List<Page> list){
        List<Page>  other= new ArrayList<>();
        for (int i=0;i<list.size();i++){
            boolean f=true;
            for (int j=0;j<childNode.size();j++){
                if(childNode.get(j).getNodeType().equals("目录")&&childNode.get(j).getPageId().equals(list.get(i).getPageId())){
                    f=false;
                }
            }
            if (f){
                other.add(list.get(i));
            }
        }
        return other;
    }
}
